package net.caixiaomi.info.adapter;

import android.support.annotation.LayoutRes;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.quickadapter.BaseQuickAdapter;
import net.caixiaomi.info.base.quickadapter.BaseViewHolder;
import net.caixiaomi.info.model.CouponsItem;

/**
 * 可用优惠券
 */

public class UserCouponAdapter extends BaseQuickAdapter<CouponsItem, BaseViewHolder> {

    public UserCouponAdapter(@LayoutRes int layoutResId) {
        super(layoutResId);
    }

    @Override
    protected void convert(BaseViewHolder helper, CouponsItem item) {
        try {

            StringBuilder stringBuilder = new StringBuilder();

            boolean noUser = TextUtils.equals(item.getUserBonusId(), "-1");
            if (!noUser) {
                stringBuilder.append(CommonApp.getInstance().getString(R.string.C_MONEY_CHAT));
            }
            stringBuilder.append(item.getBonusPrice());

            SpannableString spannableString = new SpannableString(stringBuilder);

            spannableString.setSpan(new RelativeSizeSpan(!noUser ? 1.4f : 1.0f), stringBuilder.length() - item.getBonusPrice().length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);

            helper.setText(R.id.price, spannableString);
            stringBuilder.setLength(0);

            if (!noUser) {
                stringBuilder.append(item.getMinGoodsAmount()).append("\n").append(item.getUseRange()).append("\n").append(item.getLeaveTime()).append(" ").append(item.getBonusEndTime());
                spannableString = new SpannableString(stringBuilder);
                spannableString.setSpan(new RelativeSizeSpan(0.8f), item.getMinGoodsAmount().length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                int start = stringBuilder.indexOf(item.getLeaveTime());
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.orange_primary)), start, start + item.getLeaveTime().length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                start = stringBuilder.indexOf(item.getBonusEndTime());
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.third_text)), start, start + item.getBonusEndTime().length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                helper.setText(R.id.name, spannableString);
            }

            helper.setGone(R.id.name, !noUser);

            helper.getView(R.id.btn_selected).setSelected(item.isSelected());

            helper.setGone(R.id.line, helper.getAdapterPosition() != getData().size() - 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
